﻿//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 , Jirisoft , Ltd. 
//------------------------------------------------------------

using System;
using System.Management;
using System.Net;
using System.Windows.Forms;


namespace HUA.CommonUtilities
{
    /// <summary>
    /// BUBaseSystemInfo 的定义部分
    /// 这部分其实是系统的核心部分
    /// 
    /// 修改记录
    /// 
    ///		2007.04.02 版本：3.0 JiRiGaLa   进行代码优化。
    ///		2007.01.02 版本：2.0 JiRiGaLa   进行代码优化。
    ///		2006.02.05 版本：1.1 JiRiGaLa	重新调整代码的规范化。
    ///		2004.11.19 版本：1.0 JiRiGaLa	代码创建。
    ///		
    /// 版本：3.0
    /// 
    /// <author>
    ///		<name>JiRiGaLa</name>
    ///		<date>2007.04.02</date>
    /// </author>
    /// </summary>
    public class BaseSystemInfo
    {
        public static string SUID           = string.Empty; // 单点登录唯一识别标识
        public static string UserID         = string.Empty; // 操作员代码
        public static string StaffID        = string.Empty; // 职员代码        
        public static string Username       = string.Empty; // 操作员用户名
        public static string UserRealname   = string.Empty; // 操作员全称
        public static string Code           = string.Empty; // 编号
        public static string Role           = string.Empty; // 类型
        public static string RoleFullName   = string.Empty; // 类型名称
        public static string Password       = string.Empty; // 密码

        public static string UserCompanyID          = string.Empty; // 当前的组织结构（公司代码）
        public static string UserCompanyCode        = string.Empty; // 当前的组织结构（公司编码）
        public static string UserCompanyFullName    = string.Empty; // 当前的组织结构名称（公司名称）
        public static string UserDepartmentID       = string.Empty; // 部门代码
        public static string UserDepartmentCode     = string.Empty; // 部门编号
        public static string UserDepartmentFullName = string.Empty; // 部门名称

        public static string MainAssembly          = string.Empty; // 当前的主程序
        public static string CurrentLanguage       = string.Empty; // 简体中文
        public static string Themes                = string.Empty; // 当前语言
        public static string IPAddress             = string.Empty; // IP地址
        public static string MACAddress            = string.Empty; // MAC地址        
        public static string ProcessID             = string.Empty; // 模块代码
        public static string ProcessName           = string.Empty; // 模块名称
        public static bool   IsAdministrator       = false;        // 是否系统管理员

        public static string WebServiceUsername    = "Jirisoft";   // WebService 用户名
        public static string WebServicePassword    = "Jirisoft";   // WebService 密码
        public static string WebProxy              = string.Empty; // 代理服务器

        public static bool ShowInformation         = true;         // 每个操作是否进行信息提示。

        // 当前客户公司名称
        public static string CustomerCompanyName
        {
            get
            {
                return BaseConfiguration.Instance.CustomerCompanyName;
            }
        }
        public static string CustomerPhone       = "0574-88083168";			    // 当前客户公司电话
        public static string CompanyFullName     = "宁波吉日软件开发有限公司";	// 公司名称
        public static string CompanyPhone        = "13858163011";			    // 公司电话

        // 软件名称
        public static string SoftFullName
        {
            get
            {
                return BaseConfiguration.Instance.SoftFullName;
            }
        }
        
        public static string Copyright                  = "Copyright 2003-2010 Jirisoft Corporation";
        public static string BugFeedback                = @"mailto:JiRiGaLa@HotMail.com?subject=对费用报销管理的反馈&body=这里输入您宝贵的反馈";
        public static string IEDownloadUrl              = @"http://download.microsoft.com/download/ie6sp1/finrel/6_sp1/W98NT42KMeXP/CN/ie6setup.exe";
        public static string UploadDirectory            = "Document/";
        public static bool   NeedRegister               = false;				    // 是否需要注册
        public static string ConfigUsername             = "sa";					    // 配置用户用户名
        public static string ConfigPassword             = "sa";                     // 配置用户的密码
        public static string TimeFormat                 = "HH:mm:ss";			    // 时间格式
        public static string DateFormat                 = "yyyy-MM-dd";			    // 日期短格式
        public static string DateTimeFormat             = "yyyy-MM-dd HH:mm:ss";    // 日期长格式
        public static string Version                    = "1.2";					// 软件的版本号
        public static string LastUpdate                 = "2008.07.22";				// 最后更新日期
        public static string Culture                    = "zh-CN";					// 当前语言默认设置

        public static bool Logined                  = false;    // 是否已经登录
        public static bool RecordLoginLog           = true;     // 登录历史纪录
        
        /// <summary>
        /// 是否进行日志记录
        /// </summary>
        public static bool RecordLog
        {
            get
            {
                return BaseConfiguration.Instance.RecordLog;
            }
            set
            {
                BaseConfiguration.Instance.RecordLog = value;
            }
        }

        public static bool EncryptPassword      = false;    // 是否加密登录密码
        public static bool AllowNullPassword    = true;     // 允许空密码
        public static bool SavePassword         = true;     // 登录是否保存密码
        public static bool LogException         = true;     // 是否登记异常
        public static bool LogSQL               = false;    // 是否登记数据库操作
        public static bool EventLog             = true;    // 是否登记到 Windows 系统异常中
        public static bool NeedLogin            = true;    // 是否必须登录

        public static bool MatchCase        = true;     // 是否区分大小写
        public static bool UpdateVisit      = true;     // 是否更新访问日期信息
        public static int OnLineTime0ut     = 200;      // 检查周期3分钟内不在线的，就认为是已经没在线了，生命周期检查
        public static int OnLineCheck       = 60;       // 每过1分钟，检查一次在线状态
        public static int OnLineLimit       = 0;        // 同时在线用户数量限制
        public static bool CheckOnLine      = false;    // 只允许登录一次
        public static bool CheckMACAddress  = false;    // 是否检查用户的网卡MAC地址
        public static bool CheckIPAddress   = false;    // 是否检查用户IP地址
        public static bool CheckOnLineLimit = false;    // 是否检查同时在线用户数量

        public static string AppPath        = Application.StartupPath; // 系统的路径
        public static string StartupPath    = string.Empty;
        public static string HelpNamespace  = string.Empty;
        
        // 菜单设置
        public static string RootMenuID
        {
            get
            {
                return BaseConfiguration.Instance.RootMenuID;
            }
        }
        
        public static string AppIco     = string.Empty; // 系统图标文件
        public static string FormIco    = string.Empty; // 系统图标文件
        public static bool ClientCache  = false;        // 是否采用客户端缓存
        public static bool ServerCache  = false;        // 是否采用服务器端缓存

        public static int    TopLimit       = 200;

        public static int   LockNoWaitCount             = 5;    // 锁不住记录时的循环次数
        public static int   LockNoWaitTickMilliSeconds  = 30;   // 锁不住记录时的等待时间

        // 错误报告相关
        public static string ErrorReportTo          = "JiRiGaLa_Bao@Hotmail.com";   // 发送给谁，用,;符号隔开
        public static string ErrorReportMailServer  = "smtp.qq.com";                // 邮件服务器地址
        public static string ErrorReportUsername    = "252056973@qq.com";           // 用户名
        public static string ErrorReportPassword    = "jirigala19780519";           // 密码
        public static string ErrorReportMailAddress = "252056973@qq.com";           // 来自邮件

        public static string UserCenterConnection
        {
            get
            {
                return BaseConfiguration.Instance.UserCenterConnection;
            }
            set
            {
                BaseConfiguration.Instance.UserCenterConnection = value;
            }
        }

        /// <summary>
        /// 启用权限域
        /// </summary>
        public static bool UseScopeAdmin
        {
            get
            {
                return BaseConfiguration.Instance.UseScopeAdmin;
            }
            set
            {
                BaseConfiguration.Instance.UseScopeAdmin = value;
            }
        }

        /// <summary>
        /// 启用逐级授权
        /// </summary>
        public static bool UseLicensedPermission
        {
            get
            {
                return BaseConfiguration.Instance.UseLicensedPermission;
            }
            set
            {
                BaseConfiguration.Instance.UseLicensedPermission = value;
            }
        }

        public string SoftName
        {
            get
            {
                return BaseConfiguration.Instance.SoftName;
            }
            set
            {
                BaseConfiguration.Instance.SoftName = value;
            }
        }

        // Local、WebService、Remoting
        public RunMode RunMode
        {
            get
            {
                return BaseConfiguration.Instance.RunMode;
            }
            set
            {
                BaseConfiguration.Instance.RunMode = value;
            }
        }

        // 远程服务器地址
        public static string WebService
        {
            get
            {
                return BaseConfiguration.Instance.WebService;
            }
            set
            {
                BaseConfiguration.Instance.WebService = value;
            }
        }

        public static string CommonWebService
        {
            get
            {
                return BaseConfiguration.Instance.CommonWebService;
            }
            set
            {
                BaseConfiguration.Instance.CommonWebService = value;
            }
        }

        public static string FileWebService
        {
            get
            {
                return BaseConfiguration.Instance.FileWebService;
            }
            set
            {
                BaseConfiguration.Instance.FileWebService = value;
            }
        }

        public static string WorkReportWebService
        {
            get
            {
                return BaseConfiguration.Instance.WorkReportWebService;
            }
            set
            {
                BaseConfiguration.Instance.WorkReportWebService = value;
            }
        }

        public static BaseUserInfo Login(BaseUserInfo userInfo)
        {
            if (userInfo != null)
            {
                BaseSystemInfo.SUID                     = userInfo.SUID;
                BaseSystemInfo.UserID                   = userInfo.ID;
                BaseSystemInfo.StaffID                  = userInfo.StaffID;
                BaseSystemInfo.Username                 = userInfo.Username;
                BaseSystemInfo.UserRealname             = userInfo.Realname;
                BaseSystemInfo.Code                     = userInfo.Code;
                BaseSystemInfo.Role                     = userInfo.Role;
                BaseSystemInfo.RoleFullName             = userInfo.RoleFullName;
                BaseSystemInfo.Password                 = userInfo.Password;
                BaseSystemInfo.UserCompanyID            = userInfo.CompanyID;
                BaseSystemInfo.UserCompanyCode          = userInfo.CompanyCode;
                BaseSystemInfo.UserCompanyFullName      = userInfo.CompanyFullName;
                BaseSystemInfo.UserDepartmentID         = userInfo.DepartmentID;
                BaseSystemInfo.UserDepartmentCode       = userInfo.DepartmentCode;
                BaseSystemInfo.UserDepartmentFullName   = userInfo.DepartmentFullName;
                BaseSystemInfo.IsAdministrator          = userInfo.IsAdministrator;
            }
            return userInfo;
        }

        // 当前操作员信息
        public static BaseUserInfo UserInfo
        {
            get
            {
                // 代码
                if (String.IsNullOrEmpty(UserID))
                {
                    UserID = GetIPAddress();
                }
                // 用户名
                if (String.IsNullOrEmpty(Username))
                {
                    Username = System.Environment.MachineName;
                }
                // 真实姓名
                if (String.IsNullOrEmpty(UserRealname))
                {
                    UserRealname = System.Environment.UserName;
                }

                BaseUserInfo userInfo = new BaseUserInfo();
                userInfo.SUID               = SUID;                     // 单点登录唯一识别标识
                userInfo.ID                 = UserID;                   // 操作员代码
                userInfo.StaffID            = StaffID;                  // 职员员代码
                userInfo.Username           = Username;                 // 操作员用户名
                userInfo.Realname           = UserRealname;             // 操作员全称
                userInfo.Role               = Role;                     // 类型
                userInfo.RoleFullName       = RoleFullName;             // 类型名称
                userInfo.CompanyID          = UserCompanyID;            // 公司代码
                userInfo.CompanyCode        = UserCompanyCode;          // 公司编号
                userInfo.CompanyFullName    = UserCompanyFullName;      // 公司名称
                userInfo.DepartmentID       = UserDepartmentID;         // 部门代码
                userInfo.DepartmentCode     = UserDepartmentCode;       // 部门编号
                userInfo.DepartmentFullName = UserDepartmentFullName;   // 部门名称
                userInfo.CurrentLanguage    = CurrentLanguage;          // 当前语言包
                userInfo.Themes             = Themes;                   // 当前语言
                userInfo.IPAddress          = IPAddress;                // IP地址
                userInfo.WebServiceUsername = WebServiceUsername;       // WebService用户名
                userInfo.WebServicePassword = WebServicePassword;       // WebService密码
                return userInfo;
            }
        }

        public static string GetIPAddress()
        {
            string localHostName = Dns.GetHostName();
            IPHostEntry localHostEntry = System.Net.Dns.GetHostEntry(localHostName);
            IPAddress[] addrList = localHostEntry.AddressList;
            return addrList[0].ToString();
            // 获得本机局域网IP地址
            // System.Net.IPAddress IPAddress = new System.Net.IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address);
            // return IPAddress.ToString();
        }

        public static string GetCPUSerialNo()
        {
            string cpuSerialNo = string.Empty;
            ManagementClass managementClass = new ManagementClass("Win32_Processor");
            ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
            foreach (ManagementObject managementObject in managementObjectCollection)
            {
                // 可能是有多个
                cpuSerialNo = managementObject.Properties["ProcessorId"].Value.ToString();
                break;
            }
            return cpuSerialNo;
        }

        public static string GetHardDiskInfo()
        {
            string hardDisk = string.Empty;
            ManagementClass managementClass = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
            foreach (ManagementObject managementObject in managementObjectCollection)
            {
                // 可能是有多个
                hardDisk = (string)managementObject.Properties["Model"].Value;
                break;
            }
            return hardDisk;
        }

        public static string GetMacAddress()
        {
            string macAddress = string.Empty;
            ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
            foreach (ManagementObject managementObject in managementObjectCollection)
            {
                // 可能是有多个
                if ((bool)managementObject["IPEnabled"] == true)
                {
                    macAddress = managementObject["MacAddress"].ToString();
                    break;
                }
            }
            return macAddress;
        }
    }
}